package com.homework.HomeAcount.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.homework.HomeAcount.entity.User;
import com.homework.HomeAcount.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/loadUsers")
	@RequiresPermissions("user:*")
	public String loadAcount(Model model) {
		return "user/user";
	}
	
	@RequestMapping("/loadUserList")
	@ResponseBody
	public Map<String, Object> loadUserList(HttpServletRequest request) throws UnsupportedEncodingException {
		Map<String, Object> map = new HashMap<String, Object>();
		String limit = request.getParameter("limit");
		String offset = request.getParameter("offset");
		String userCode = StringUtils.trimToNull(request.getParameter("userCode"));
		String username = StringUtils.trimToNull(request.getParameter("username"));
		String locked = StringUtils.trimToNull(request.getParameter("locked"));
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if(null != userCode) {
			//中文乱码解决
			userCode = new String(userCode.getBytes("ISO-8859-1"),"UTF-8");
		}
		paramMap.put("userCode", userCode);
		paramMap.put("username", username);
		paramMap.put("locked", locked);
		int pageSize = Integer.valueOf(limit);
		int pageNumber = Integer.valueOf(offset);
		paramMap.put("pageNumber", pageNumber);
		paramMap.put("pageSize", pageSize);
		int total = userService.getUserListSize(paramMap);
		List<User> list = userService.loadUserList(paramMap);
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	@RequestMapping("/loadUser")
	@ResponseBody
	public User loadUserInfoById(String id) {
		User user = userService.loadUserInfoById(id);
		return user;
	}
	
	@RequestMapping("/addUser")
	@ResponseBody
	public Map<String, Object> addUser(User user) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			userService.addUser(user);
			map.put("flag", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("flag", false);
		}
		return map;
	}
	
	@RequestMapping("/updateUser")
	@ResponseBody
	public Map<String, Object> updateUser(User user) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			userService.updateUser(user);
			map.put("flag", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("flag", false);
		}
		return map;
	}
	
	@RequestMapping("/deleteUser")
	@ResponseBody
	public Map<String, Object> deleteUser(String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			userService.deleteUser(id);
			map.put("flag", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("flag", false);
		}
		return map;
	}
}
